<template>
  <view class="tool">
    <view class="n-card-top">
      <view class="left">
        <image src="../../../static/images/home_icon_3.png" mode="widthFix" />
        <view class="fw600">常用工具</view>
      </view>
      <view class="right" @click="toTool" v-if="list.length">
        <span class="fs28 c2">全部</span>
        <image src="../../../static/icons/icon-more.png" mode="widthFix" />
      </view>
    </view>
    <view class="tools" v-if="list.length">
      <template v-for="(item, index) in list" :key="index">
        <view class="tool-item" v-if="index < 4" @click="nav(item)">
          <image :src="item.image" mode="widthFix"></image>
          <span class="fs26 c1 fw500">{{ item.name }}</span>
          <view v-if="item.tagName" class="tool-tag">{{ item.tagName }}</view>
        </view>
      </template>
    </view>
    <!-- 全部没有权限时 -->
    <view class="" v-else>
      <ve-empty type="auth" text="暂无权限"></ve-empty>
      <ve-gap size="20"></ve-gap>
    </view>
  </view>
</template>

<script setup lang="ts">
  import { ref, onMounted } from 'vue'
  import { userStore } from '@/stores'
  import { appConfig } from '@/config/index.js'
  import { listJS } from '@/pages/index/menu.js'

  const userInfoStore = userStore()
  const list = ref<any[]>([])
  const getTool = async () => {
    const permissions = userInfoStore.permissons
    // appConfig.asyncRoutes 调试时写true显示全部按钮
    if (appConfig.asyncRoutes == false) {
      list.value = listJS
      return
    }
    // 异步路由通过权限过滤菜单
    list.value = listJS.filter((it) => {
      return permissions.includes(it.permission)
    })
    const ret = list.value.findIndex((ev) => {
      return ev.show == true
    })
  }
  const nav = (item) => {
    if (item.nav == 'navigateTo') {
      uni.navigateTo({ url: item.url })
    }
  }
  const toTool = () => {
    uni.navigateTo({ url: '/pages/index/tool' })
  }
  onMounted(() => {
    getTool()
  })
  defineExpose({
    getTool,
  })
</script>

<style lang="scss" scoped>
  .tool {
    margin: 24rpx 24rpx 0;
    background-color: #f7f8fa;
    box-shadow: 0 8rpx 20rpx 0 rgba(128, 128, 128, 0.05);
    border-radius: 8rpx;
    .n-card-top {
      padding: 28rpx 0;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .left {
        display: flex;
        justify-content: flex-start;
        align-items: center;
        image {
          width: 40rpx;
          height: 40rpx;
          margin-right: 16rpx;
        }
      }
      .right {
        display: flex;
        justify-content: flex-end;
        align-items: center;
        image {
          margin-left: 16rpx;
          width: 10rpx;
        }
      }
    }
    .tools {
      width: 100%;
      display: flex;
      justify-content: flex-start;
      align-items: center;
      height: 168rpx;
      margin-bottom: 40rpx;
      .tool-item {
        position: relative;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        width: calc((100% - 60rpx) / 4);
        padding: 24rpx 0;
        background: #ffffff;
        margin: 12rpx 20rpx 12rpx 0;
        box-shadow: 0 4rpx 14rpx 0 rgba(128, 128, 128, 0.05);
        border-radius: 8rpx;
        &:nth-of-type(4n) {
          margin-right: 0;
        }
        image {
          width: 96rpx;
          height: 96rpx;
          margin-bottom: 8rpx;
        }
        .tool-tag {
          position: absolute;
          top: 0;
          right: 0;
          width: 68rpx;
          height: 40rpx;
          line-height: 40rpx;
          text-align: center;
          color: #ffffff;
          font-size: 24rpx;
          border-radius: 0 8rpx 0 8rpx;
          background: linear-gradient(270deg, #ff554c 0%, #ffcb21 100%);
        }
      }
    }
  }
  .mt20 {
    margin-top: 20rpx;
  }
</style>
